const request = require('../../../../../utils/request')
const util = require('../../../../../utils/util');
let CodeUrlbg = request.BASE_URL + '/static/images/qrcodebg.png'; //背景图片
let logo = request.BASE_URL + 'static/images/new-logo.png'

let white = request.BASE_URL + '/static/images/new-tg-white.png'

let ctx, canvas;
Page({
    data: {
        navbarHeight: '',
        width: 0,
        height: 0,
        rpx: 0,
        recom_code: '',
        qrcode: '',
        shaerPoster: ''
    },

    onLoad: function () {
        let _this = this
        wx.getSystemInfo({
            success: function (res) {
                console.log(res, 34343)
                _this.setData({
                    navbarHeight: util.getNavbarHeight(),
                    statusBarHeight: res.statusBarHeight,
                    // 屏幕宽高各乘2倍, 宽高保存到data, 供html用
                    width: res.screenWidth,
                    height: res.screenHeight,
                    rpx: res.windowWidth / 750,

                    screen_width: res.windowWidth,
                    screen_height: res.windowHeight
                })

            }
        })
        this.getTuiguang()
    },
    onReady() {

    },
    getTuiguang() {
        // request._post('/api?method=user.tuiguangma', {

        // }).then(res => {
        //     this.setData({
        //         recom_code: res.data.recom_code,
        //         qrcode: res.data.qrcode,
        //     })
        //     const query = wx.createSelectorQuery()
        //     query.select('#myCanvas').fields({
        //         id: true,
        //         node: true,
        //         size: true
        //     }).exec(this.init.bind(this))
        // })
        this.setData({
          recom_code: '39a50a2e',
          qrcode: 'https://zhimeijia.oss-cn-qingdao.aliyuncs.com/static/uploads/images/2024/02/01/170675995565bb17139480d.jpg',
      })
      const query = wx.createSelectorQuery()
      query.select('#myCanvas').fields({
          id: true,
          node: true,
          size: true
      }).exec(this.init.bind(this))
    },
    //初始化画布
    init(res) {
        canvas = res[0].node
        ctx = canvas.getContext('2d')
        const dpr = wx.getSystemInfoSync().pixelRatio; //获取屏幕的像素比  值为2
        //新接口需显示设置画布宽高； w*2 h*2
        canvas.width = res[0].width * dpr;
        canvas.height = res[0].height * dpr;
        ctx.scale(dpr, dpr); //缩放

        //向画布载入图片的方法
        this.canvasDraw(ctx, canvas).then(res => {
            //图片头像渲染完成之后，渲染文字
            // this.drawText(ctx)
        })

    },
    // 绘制背景图
    canvasDraw(ctx, canvas) {
        let _this = this,
            rpx = this.data.rpx
        console.log(this.data.navbarHeight)

        return new Promise(resolve => {
            let img = canvas.createImage(); //创建img对象
            img.src = CodeUrlbg;
            img.onload = () => {
                ctx.drawImage(img, 0, 0, _this.data.width, _this.data.height);
                let img_white = canvas.createImage()
                img_white.src = white
                img_white.onload = () => {
                    ctx.drawImage(img_white, 100 * rpx, (284 * rpx), 575 * rpx, 726 * rpx);
                    let img_logo = canvas.createImage()
                    img_logo.src = logo
                    img_logo.onload = () => {
                        ctx.drawImage(img_logo, 305 * rpx, (232 * rpx), 140 * rpx, 140 * rpx);
                        // 添加文字
                        ctx.font = 32 * rpx + 'px Arial'
                        ctx.fillStyle = '#D12B2B';
                        ctx.fillText('·至美家·', (315 * rpx), 422 * rpx)

                        ctx.font = 16 * rpx + 'px Arial'
                        ctx.fillStyle = '#D12B2B';
                        ctx.fillText('ZHIMEIJIA', 335 * rpx, 452 * rpx)

                        // ctx.font = 26 * rpx + 'px PingFang SC'
                        // ctx.fillStyle = '#999999';
                        // ctx.fillText('推广码：' + _this.data.recom_code, 277 * rpx, 500 * rpx)

                        // 小程序码
                        let img_qrcode = canvas.createImage()
                        img_qrcode.src = _this.data.qrcode
                        // 
                        img_qrcode.onload = () => {
                            ctx.drawImage(img_qrcode, 200 * rpx, 540 * rpx, 380 * rpx, 380 * rpx);
                            ctx.font = 26 * rpx + 'px PingFang SC'
                            ctx.fillStyle = '#999999';
                            ctx.fillText('使用微信扫描二维码自动绑定', 220 * rpx, 960 * rpx)
                            wx.canvasToTempFilePath({

                                canvasId: 'canvas_box',
                                canvas: canvas,
                                success(res) {
                                    console.log('二维码临时路径为：', res.tempFilePath)
                                    _this.setData({
                                        shaerPoster: res.tempFilePath
                                    })
                                },
                                fail(res) {
                                    console.error(res)
                                }
                            })
                        }



                    };
                };



            };




        })

    },
    downloadPoster: function () {
        let page = this,
            image = page.data.shaerPoster
        if (image) {
            wx.saveImageToPhotosAlbum({
                filePath: image,
                success(res_) {
                    wx.showToast({
                        title: '保存图片成功',
                    })
                },
                fail(res_) {
                    wx.showToast({
                        title: '保存图片失败',
                        icon: 'none'
                    })
                }
            })
            // wx.getSetting({
            //     success(res) {
            //         if (!res['authSetting']['scope.writePhotosAlbum']) {
            //             //引导用户授权
            //             wx.authorize({
            //                 scope: 'scope.writePhotosAlbum',
            //                 success() {
            //                     wx.downloadFile({
            //                         url: image,
            //                         success(response) {

            //                             if (response.statusCode == 200) {

            //                             }
            //                         },
            //                         fail(response) {
            //                             console.log(response,'Dfjkljfdf')
            //                             // wx.showToast({
            //                             //     title: '接口调用失败',
            //                             //     icon: 'none'
            //                             // })
            //                         }
            //                     })
            //                 }
            //             })
            //         } else {
            //             console.log(image,34334343434)
            //             //已授权，直接下载
            //             wx.downloadFile({
            //                 url: image,
            //                 success(response) {
            //                     if (response.statusCode == 200) {
            //                         wx.saveImageToPhotosAlbum({
            //                             filePath: response.tempFilePath,
            //                             success(res_) {
            //                                 wx.showToast({
            //                                     title: '保存图片成功',
            //                                 })
            //                             },
            //                             fail(res_) {
            //                                 wx.showToast({
            //                                     title: '保存图片失败',
            //                                     icon: 'none'
            //                                 })
            //                             }
            //                         })
            //                     }
            //                 },
            //                 fail(response) {
            //                     console.log(response,'Dfjkljfdf')
            //                     // wx.showToast({
            //                     //     title: '接口调用失败',
            //                     //     icon: 'none'
            //                     // })
            //                 }
            //             })
            //         }
            //     }
            // })
        }
    },
    toBack() {
        wx.navigateBack()
    }
})